﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCExtensions;
using NBlock.Core;

namespace Cost.Controllers
{
    public class RevenueManageController : Controller
    {
        #region 构造函数
        private readonly ICostRepository<Revenue> revenueRepository;
        private readonly ICostRepository<RevenueItem> revenueItemRepository;
        private readonly ICostRepository<DishOrderItem> dishOrderItemRepository;

        public RevenueManageController(ICostRepository<Revenue> revenueRepository,
            ICostRepository<RevenueItem> revenueItemRepository,
            ICostRepository<DishOrderItem> dishOrderItemRepository)
        {
            this.revenueRepository = revenueRepository;
            this.revenueItemRepository = revenueItemRepository;
            this.dishOrderItemRepository = dishOrderItemRepository;
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(string Id, DateTime? date)
        {
            var data = revenueRepository.Find(Id) ?? new Revenue();
            data.Date = date ?? DateTime.Now.Date;
            data.SiteId = nbUserContext.Current.CurrentUser.UnitCode;

            ViewBag.RevenueList = GetItems(Id, data.SiteId, data.Date.Value);
            return View(data);
        }

        private IList<RevenueItem> GetItems(string orderId, string siteId, DateTime date)
        {
            var list = revenueItemRepository.FindAll(d => d.OrderId == orderId).ToList();
            // 从下周订单、补充订单中产生明细
            if (list.Count == 0)
            {
                string SqlStr = string.Format(@"select DishId Id,'' OrderId,DishId,Category,Date,Code,Name,SiteId,sum(Count) OrderCount,0 SurplusCount,0.0 SellMoney,0 SellCount   from DishOrderItem 
                                    where SiteId = '{0}' and Date = '{1}'
                                    group by DishId,Category,Date,Code,Name,SiteId order by Category,Date,Name"
                    , siteId, date);
                var newlist = ((CostRepository<DishOrderItem>)dishOrderItemRepository).Context.Database.SqlQuery<RevenueItem>(SqlStr).ToList();
                return newlist;
            }

            return list;
        }

        [HttpPost]
        public JsonResult Save(string Id, IList<RevenueItem> RevenueList)
        {
            var data = revenueRepository.Find(Id) ?? new Revenue();
            TryUpdateModel(data);
            var ItemDesc = string.Empty;

            // 先删除所有子表，再保持所有子表数据
            revenueItemRepository.DeleteAll(m => m.DishId == data.Id);
            if (RevenueList != null)
            {
                foreach (var m in RevenueList)
                {
                    m.OrderId = data.Id;
                    ItemDesc += string.Format("{0}，", m.Name);
                    revenueItemRepository.Insert(m);
                }
            }
            data.ItemDesc = ItemDesc.Trim('，');
            revenueRepository.Save(data);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult Delete(string Id)
        {
            revenueRepository.Delete(Id);
            revenueItemRepository.DeleteAll(m => m.OrderId == Id);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult DeleteAll(string Ids)
        {
            var items = Ids.Split(',');
            revenueRepository.DeleteAll(d => items.Contains(d.Id));
            revenueItemRepository.DeleteAll(m => items.Contains(m.OrderId));
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult List(string IsFilter, nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = revenueRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
    }
}
